import os

from flask_admin import Admin
from flask_admin.menu import MenuLink
from flask_login import current_user

from app.database.models import (
    AdminUser,
    AllowedIP,
    BotSettings,
    Category,
    Country,
    Order,
    Payment,
    Proxy,
    ProxyProvider,
    Server,
    Site,
    Statistics,
    User,
    VPNServer,
)
from app.server.flask_app import db
from app.server.flask_app.admin_views import (
    AdminUserBaseModelview,
    CategoryModelView,
    MyAdminIndexView,
    MyBaseModelView,
    MyFileAdmin,
    OrderModelView,
    PaymentsModelView,
    ProxyModelView,
    ServerModelView,
    SitesModelView,
    UserModelView,
    VPNServerModelView,
)


# # Only show the Logout link if the user is logged in.
class LogoutMenuLink(MenuLink):
    def is_accessible(self):
        return current_user.is_authenticated


def init_admin_panel(app):
    admin = Admin(
        app,
        name="Admin Dashboard",
        base_template="myadmin3/my_master.html",
        template_mode="bootstrap4",
        index_view=MyAdminIndexView(),
    )

    admin.add_view(
        ProxyModelView(
            Proxy,
            db.session,
            name="Proxies",
            menu_icon_type="fas",
            menu_icon_value="fa-server",
            endpoint="proxy",
        )
    )
    admin.add_view(
        UserModelView(
            User,
            db.session,
            name="Users",
            menu_icon_type="fas",
            menu_icon_value="fa-user",
            endpoint="user",
        )
    )
    admin.add_view(
        CategoryModelView(
            Category,
            db.session,
            name="Categories",
            menu_icon_type="fas",
            menu_icon_value="fa-tags",
            endpoint="category",
        )
    )
    admin.add_view(
        SitesModelView(
            Site,
            db.session,
            name="Sites",
            menu_icon_type="fas",
            menu_icon_value="fa-th-list",
            endpoint="subcategory",
        )
    )
    admin.add_view(
        MyBaseModelView(
            ProxyProvider,
            db.session,
            name="Proxy providers",
            menu_icon_type="fas",
            menu_icon_value="fa-phone",
            endpoint="provider",
        )
    )
    admin.add_view(
        ServerModelView(
            Server,
            db.session,
            name="Servers",
            menu_icon_type="fas",
            menu_icon_value="fa-cloud",
            endpoint="server",
        )
    )
    admin.add_view(
        VPNServerModelView(
            VPNServer,
            db.session,
            name="VPN Servers",
            menu_icon_type="fas",
            menu_icon_value="fa-cloud",
            endpoint="vpn_server",
        )
    )
    admin.add_view(
        MyBaseModelView(
            Country,
            db.session,
            name="Countries",
            menu_icon_type="fas",
            menu_icon_value="fa-globe",
            endpoint="country",
        )
    )
    admin.add_view(
        OrderModelView(
            Order,
            db.session,
            name="Orders",
            menu_icon_type="fas",
            menu_icon_value="fa-shopping-cart",
            endpoint="order",
        )
    )
    admin.add_view(
        MyBaseModelView(
            AllowedIP,
            db.session,
            name="Allowed ips",
            menu_icon_type="fas",
            menu_icon_value="fa-mobile",
            endpoint="allowed_ip",
        )
    )
    admin.add_view(
        PaymentsModelView(
            Payment,
            db.session,
            name="Payments",
            menu_icon_type="fas",
            menu_icon_value="fa-money",
            endpoint="payment",
        )
    )
    admin.add_view(
        MyBaseModelView(
            BotSettings,
            db.session,
            name="Bot settings",
            menu_icon_type="fas",
            menu_icon_value="fa-gears",
            endpoint="bot_settings",
        )
    )
    admin.add_view(
        MyBaseModelView(
            Statistics,
            db.session,
            name="Statistics",
            menu_icon_type="fas",
            menu_icon_value="fa-bar-chart",
            endpoint="statistic",
        )
    )
    # admin.add_view(
    #     ProfileView(
    #         name="My Profile",
    #         menu_icon_type="fas",
    #         menu_icon_value="fa-address-book",
    #         endpoint="profile",
    #     )
    # )
    admin.add_view(
        AdminUserBaseModelview(
            AdminUser,
            db.session,
            name="Admin users",
            menu_icon_type="fas",
            menu_icon_value="fa-users",
            endpoint="admin-user",
        )
    )

    # log_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "logs")
    log_path = os.path.join(
        os.path.dirname(os.path.dirname(os.path.dirname(__file__))), "bot/logs"
    )

    admin.add_view(
        MyFileAdmin(
            log_path, name="Log Files", menu_icon_type="fas", menu_icon_value="fa-copy"
        )
    )
